package com.mbyte.easy.rest.vacation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.vacation.entity.Vacation;
import com.mbyte.easy.vacation.service.IVacationService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/vacation")
public class RestVacationController extends BaseController  {

    @Autowired
    private IVacationService vacationService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param vacation
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String startTimeSpace, String endTimeSpace, String createTimeSpace, String deleteTimeSpace, Vacation vacation) {
        Page<Vacation> page = new Page<Vacation>(pageNo, pageSize);
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<Vacation>();

        if(vacation.getUserId() != null  && !"".equals(vacation.getUserId() + "")) {
            queryWrapper = queryWrapper.like("user_id",vacation.getUserId());
         }


        if(vacation.getReason() != null  && !"".equals(vacation.getReason() + "")) {
            queryWrapper = queryWrapper.like("reason",vacation.getReason());
         }


        if(vacation.getStartTime() != null  && !"".equals(vacation.getStartTime() + "")) {
            queryWrapper = queryWrapper.like("start_time",vacation.getStartTime());
         }


        if(vacation.getEndTime() != null  && !"".equals(vacation.getEndTime() + "")) {
            queryWrapper = queryWrapper.like("end_time",vacation.getEndTime());
         }


        if(vacation.getVacationStatus() != null  && !"".equals(vacation.getVacationStatus() + "")) {
            queryWrapper = queryWrapper.like("vacationStatus",vacation.getVacationStatus());
         }


        if(vacation.getVerifyStatus() != null  && !"".equals(vacation.getVerifyStatus() + "")) {
            queryWrapper = queryWrapper.like("verifyStatus",vacation.getVerifyStatus());
         }


        if(vacation.getIsVerified() != null  && !"".equals(vacation.getIsVerified() + "")) {
            queryWrapper = queryWrapper.like("isVerified",vacation.getIsVerified());
         }


        if(vacation.getDeleteTime() != null  && !"".equals(vacation.getDeleteTime() + "")) {
            queryWrapper = queryWrapper.like("delete_time",vacation.getDeleteTime());
         }


        if(vacation.getDeleteStatus() != null  && !"".equals(vacation.getDeleteStatus() + "")) {
            queryWrapper = queryWrapper.like("delete_status",vacation.getDeleteStatus());
         }

        IPage<Vacation> pageInfo = vacationService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  vacation);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param vacation
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Vacation vacation){
        return toAjax(vacationService.save(vacation));
    }

    /**
    * 添加
    * @param vacation
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Vacation vacation){
        return toAjax(vacationService.updateById(vacation));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(vacationService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(vacationService.removeByIds(ids));
    }

}

